//优惠套餐绑定事件处理


//用户可操作的事件：选中事件，数量判断事件，加入套餐事件，从套餐中删除事件
var Bind_Num=0;
var cartbtnsumitName="#ark_addToCart";
var bind_goods_spec_json=[];
var sellProperty={};
var bind_goods_tpl="";
var bind_nogoods_tpl="";

//lodo_bind_Meal_list格式：
/*
	{
		"1":{bid:商品ID,oaid:商品规格ID,price:商品价格,pic:商品图片,buyqn:购买数量},
		"2":{bid:商品ID,oaid:商品规格ID,price:商品价格,pic:商品图片,buyqn:购买数量}
	}

*/
var lodo_bind_Meal_list={};


//事件处理
var BIND_Meal=(function(){
	   //var f="\u6B63\u5728\u6DFB\u52A0\u4F18\u60E0\u5957\u9910\u5230\u8D2D\u7269\u8F66...\u8BF7\u7A0D\u5019";//正在添加优惠套餐到购物车...请稍候
	   //var lang_MAXQN="\u5BF9\u4E0D\u8D77\uFF0C\u8BE5\u5546\u54C1\u53EF\u9009\u7684\u6700\u5927\u6570\u91CF\u4E3A";//对不起，该商品可选的最大数量为
	   
	   //var P="\u5546\u54c1\u5df2\u6dfb\u52a0\u5230\u8d2d\u7269\u8f66\uff01";//商品已添加到购物车！
	   //var lang_PJ="\u8BF7\u9009\u62E9\u914D\u4EF6\uFF1A";//请选择配件
	   //var lang_PS="\u914D\u4EF6\uFF1A\"{name}\"\u6570\u91CF\u53EA\u80FD\u5728{minnum}\u548C{maxnum}\u4E4B\u95F4";//配件："{name}"数量只能在{minnum}和{maxnum}之间!
	   //var lang_lv="\u8D2D\u4E70\u6570\u91CF\u53EA\u80FD\u662F\u4E00\u624B\u7684\u6574\u6570\u500D";//购买数量只能是一手的整数倍！

	   var Q=lodo_lang_js.bind_goods.tip_1;//此商品缺货中！
	   var N=lodo_lang_js.bind_goods.tip_2;//请选择：
	   var H=lodo_lang_js.bind_goods.tip_4;//您所填写的商品数量超过库存！
	   var K=lodo_lang_js.bind_goods.tip_5;//请填写正确的商品数量！
	   var U=lodo_lang_js.bind_goods.tip_6;//您是卖家,不能添加到购物车！
	var O=new cart_r_cls();
					   
   var I=function(g){
	   O.show($(cartbtnsumitName));
	   O.reportFailed(g)
	  };
   var E=function(){
		O.hide();
		$(cartbtnsumitName).show();
	   var j=$("#ark_listCart");
	   if(!j[0]){I(U);return false}
	   if(typeof(sellProperty[Bind_Num])!="undefined"){
		   if(!sellProperty[Bind_Num].isAllPVSelected()){
			   var k=sellProperty[Bind_Num].getUnselectedPVNames().join('", "');
			   I(N+' "'+k+'"');
			   $("#bind_"+Bind_Num+"_info-hx").html("<em>"+N+' "'+k+'"</em>');
			   return false
			}
		}
		var m=$("#bind_"+Bind_Num+"_buy_maxCount");
		var l=$("#bind_"+Bind_Num+"_buyqn");
		var buy_minqn=$("#bind_"+Bind_Num+"_minbuyqn");
	    var buy_maxqn=$("#bind_"+Bind_Num+"_maxbuyqn");
			
		if(!m[0]){return false}
		if(!l[0]){return false}
		if(!buy_minqn[0]){return false}
		if(!buy_maxqn[0]){return false}
		var i=/^[1-9]\d*$/;
		if(!i.test(l.val().replace(/(^\s*)|(\s*$)/g,""))){
			I(K);
			return false
			}
		var h=parseInt(l.val());
		var g=parseInt(m.val());
		var bminqn=parseInt(buy_minqn.val());
		var bmaxqn=parseInt(buy_maxqn.val());
		//var lv=l.attr("lv");
		if(g==0){I(Q); return false;}
		
/*		if ( h<=0 || (h % lv) != 0)
		{
			I(lang_lv);
			return false;
		}*/
		if(h>g&&g!=-1){I(H);return false}
		
		if(!$.isEmptyObject(lodo_bind_Meal_list[Bind_Num])){h+=(lodo_bind_Meal_list[Bind_Num].buyqn);}
		//判断是否已经选择了,如果选择了判断是否已经是最大值
		if(h<bminqn){I(lodo_lang_js.bind_goods.tip_7.replace("{$1}",bminqn));return false}
		if(h>bmaxqn){I(lodo_lang_js.bind_goods.tip_8.replace("{$1}",bmaxqn));return false}
		var buyqn_total=h;
		for(var B in lodo_bind_Meal_list){
			if(!$.isEmptyObject(lodo_bind_Meal_list[B])&&Bind_Num!=B){
				buyqn_total+=lodo_bind_Meal_list[B].buyqn;
			}
		}
		var bind_buyQn_Total=$("#bind_buyQn_Total").val()*1;
		if(buyqn_total>bind_buyQn_Total){I(lodo_lang_js.bind_goods.tip_9.replace("{$1}",bind_buyQn_Total));return false}
		
		return true
		};
	var a={};
	a.attachEventToAddTrigger=function(){
		$("div[id$='_sellProperty_area']").hover(
		  function(){
				$(this).addClass("no-selceted");
				return false;
			},
		  function(){
				$(this).removeClass("no-selceted");
				return false;
		})
		
		$(".btn_addToMeal").bind("click",function(){
					Bind_Num=$(this).attr("bindnum");
				   if(!!E()){
					   cartbtnsumitName=this;
					   a.addToMeal(Bind_Num);
					}
					return false;
				})
				.bind("mouseover",function(){
					Bind_Num=$(this).attr("bindnum");
					cartbtnsumitName=this;
					E();//需重复两次才起作用
					if(!E()){
						$(this).css("cursor","not-allowed");
					}else{
						$(this).css("cursor","");
						if($("#bind_addToMealIndicator")[0]){O.hide()}
					}
				})
				.bind("mouseout",function(){
					Bind_Num=$(this).attr("bindnum");
					cartbtnsumitName=this;
					$(this).css("cursor","");
					O.hide();
				});
		
		//购买数量+1事件
		$(".bind_addgoodsnum").bind("click",function(){
			 bindnum=$(this).attr("bindnum");
			 good_cal(1,bindnum);
		 });
		//购买数量-1事件
		$(".bind_lessgoodsnum").bind("click",function(){
			 bindnum=$(this).attr("bindnum");
			 good_cal(-1,bindnum);
		 });
		
		
		
		
		$(".baddtocart").bind("click",function(){
			cartbtnsumitName=this;
			var bind_id=$(this).attr("bgid");
			var url="";
			var btotalNum=0;
			var buyqn_total=0;
			for(var I in lodo_bind_Meal_list){
				var tmp_meal=lodo_bind_Meal_list[I];
				if(!$.isEmptyObject(tmp_meal)){
					btotalNum++;
					if(url!=""){url+="&"}
					url+="buyno_"+tmp_meal.bid+"=1";
					url+="&oaid_"+tmp_meal.bid+"="+tmp_meal.oaid;
					url+="&buyqn_"+tmp_meal.bid+"="+tmp_meal.buyqn;
					buyqn_total+=tmp_meal.buyqn;
				}
			}
			var bind_buyQn_Total=$("#bind_buyQn_Total").val()*1;
			if(buyqn_total!=bind_buyQn_Total){alert(lodo_lang_js.bind_goods.tip_10.replace("{$1}",bind_buyQn_Total));return false;}
			if(url!=""){url="&"+url;}
			url=lodo_web_path+"addbindgoodstocart.asp?act=add&gid="+bind_id+url;
/*			alert(url);
			return false;*/
			a.detachEventFromAddTrigger();
			var option={
				action:function(){
					$.getJSON(url,function(data){
						O.hide();
						$(cartbtnsumitName).show();
						a.attachEventToAddTrigger();
						if(data.errno){alert(data.msg);return false;}
						if(data.gotono){location.href=data.gotourl};
						if(data.logno){
							$loginFrame({
								type:"func",
								check: false,
								checkReady:false,
								model: false,
								title:lodo_lang_js.bind_goods.tip_11,
								gotourl:data.gotourl,
								action:option.action
							});
							return false;
						}
						if(data.cartno){
								O.show($(cartbtnsumitName),{isReportSuccess:"true"});
								O.reportSuccess(data);
								$("#ark_continueShopping,#ark_closeIndicator").live("click",function(){
									  O.hide();
									  return false;
								  });
							return false;
						}
					})
				}
			}
			option.action();
		})

	};
	a.detachEventFromAddTrigger=function(){
		$(".btn_addToMeal,.baddtocart").unbind("click");
		//$(".btn_addToMeal,.baddtocart").click(function(){return false;});
		O.show($(cartbtnsumitName));
		O.reportFailed("<img src='images/loading3.gif'>"+lodo_lang_js.bind_goods.tip_12);
		$(cartbtnsumitName).hide();
		
	};
	
	//将选中的商品加入到套餐里
	a.addToMeal=function(Num){
/*		this.detachEventFromAddTrigger();*/
	   var buy_bid=$("#bind_"+Num+"_buy_itemId").val();
	   var buy_oaid=$("#bind_"+Num+"_buy_itemDetailId").val();
	   var buy_qn=$("#bind_"+Num+"_buyqn").val()*1;
		
		//判断是否已经选择了,如果选择了判断是否已经是最大值
		var addno=false;
		if($.isEmptyObject(lodo_bind_Meal_list[Num])){addno=true;}
		if(addno){
			var goods_pic=$("#bind_"+Num+"_goodspic").attr("src");
			var goods_price=$("#bind_"+Num+"_price1>em").html()*1;
			if(buy_oaid==""){goods_price=$("#bind_"+Num+"_price2").attr("gprice")*1;}
			lodo_bind_Meal_list[Num]={bid:buy_bid,oaid:buy_oaid,price:goods_price,buyqn:buy_qn,pic:goods_pic};
		}else{
			var addbuyqn=(buy_qn*1)+(lodo_bind_Meal_list[Num].buyqn*1);
			lodo_bind_Meal_list[Num].buyqn=addbuyqn;
		}
		
		//加入到要购买的套餐框中去
		var selkitinfo=$("#selkitinfo_"+Num);
		if(selkitinfo[0]){
			if(addno){
				var lodo_bind_goods_spec_info="";
				if(!$.isEmptyObject(sellProperty[Num]))
				{
					var tmp_sellProps=sellProperty[Num].sellProps;
					for(var A in tmp_sellProps)
					{
						var C=sellProperty[Num].selectPVID[A];
						if(lodo_bind_goods_spec_info!=""){lodo_bind_goods_spec_info+="<br>"}
						lodo_bind_goods_spec_info+=tmp_sellProps[A].name+":"+tmp_sellProps[A].pvs[C].name;
					}
				}
				tmp_tpl=bind_goods_tpl
				tmp_tpl=tmp_tpl.replaceAll("\{\\$lodo_bind_goods_pic\}",lodo_bind_Meal_list[Num].pic);
				tmp_tpl=tmp_tpl.replaceAll("\{\\$lodo_bind_num\}",Num);
				tmp_tpl=tmp_tpl.replaceAll("\{\\$lodo_bind_goods_spec_info\}",lodo_bind_goods_spec_info);
				selkitinfo.html(tmp_tpl);
			}
			
			Total_Goods_OriginalPrice();
			$("#Qty_"+Num).html(lodo_bind_Meal_list[Num].buyqn);
			
		}
		

		return false;
	};
	a._init=function(){
		this.attachEventToAddTrigger();
		};
	a._init();
	return a  
});

//计算已在套餐的商品原价格
function Total_Goods_OriginalPrice(){
	var bind_originalprice=0;
	for(var I in lodo_bind_Meal_list){
		var tmp_meal=lodo_bind_Meal_list[I];
		if(!$.isEmptyObject(tmp_meal)){
			bind_originalprice+=tmp_meal.buyqn*tmp_meal.price;
		}
	}
	$("#bind_originalprice").html(FormatNumber(bind_originalprice,2));
}

//从套餐中删除所选的商品
function RemoveTKNKit(Num){
	lodo_bind_Meal_list[Num]={};
	var selkitinfo=$("#selkitinfo_"+Num);
	var tmp_tpl=bind_nogoods_tpl;
	tmp_tpl=tmp_tpl.replaceAll("\{\\$LODO_BIND_NUM\}",Num);
	selkitinfo.html(tmp_tpl);
	Total_Goods_OriginalPrice();
}


$(function(){
	$.each(bind_goods_spec_json,function(i,items){
		var sellProps=items["sellProps"];
		var allItems=items["allItems"];
		var mealDetailPrice = [];
		var defaultSelect = {};//{"21":"21_25","22":"22_28","23":"23_30"}
		var tmp_bindNum=(i+1);
		var p_id=allItems[0].itemid;
		var tmp_idPrefix="#bind_"+tmp_bindNum+"_";
		if(!$.isEmptyObject(sellProps)){//当有规格时
			sellProperty[tmp_bindNum] = new spec_SellProperty(tmp_idPrefix,sellProps, allItems, function(argument) {
				var way = argument.way || 1;
				var pvSelected = argument.selected || false;
				var pvid = argument.pvid || '';
				var currentPV = argument.currentPV || '';
				var name = argument.name || '';
				var itemMealPvid = pvid;
				var defaultItemId=argument.defaultItemId;
				//默认值获取
				
				var tmp_pvid=pvid.split("_");
				var ark_spec_value=$(this.idPrefix+"spec_value_"+tmp_pvid[0]+"[way!='3']");
				if(ark_spec_value[0]){
					if(pvSelected){
						ark_spec_value.html(name);
					}else{
						ark_spec_value.html(ark_spec_value.attr("tip"));
					}
					if(way!=1){ark_spec_value.parent().click();}
				}
				if ($(this.idPrefix+"info-hx")[0]) {
					if (this.isNoPVSelected()) {
						var msg = this.getAllPVNames().join('", "');
						$(this.idPrefix+"info-hx").html(lodo_lang_js.bind_goods.tip_2+' "' + msg + '"');
					} else {
						var msg = this.getSelectedValueNames().join('", "');
						$(this.idPrefix+"info-hx").html(lodo_lang_js.bind_goods.tip_3+'<em>"'+ msg +'"</em>');
					}
				}
				$("#bind_addToMealIndicator").hide();
			});
			sellProperty[tmp_bindNum].setDefaultItemId(allItems[0].itemid);
			sellProperty[tmp_bindNum].setDefaultSelect(defaultSelect);
			sellProperty[tmp_bindNum].setSellPropsCount(items.length)//商品规格数量
			sellProperty[tmp_bindNum].initload();
		}else{
			var lodo_goods_quantity=$(tmp_idPrefix+"buy_maxCount").val()*1;
			$(tmp_idPrefix+"price1").hide();
			$(tmp_idPrefix+"price2").show();
			if(lodo_goods_quantity!=-1){
				if(lodo_goods_quantity<=0){
					$(tmp_idPrefix+"stock>em").html(lodo_lang_js.bind_goods.tip_1);
					$(tmp_idPrefix+"stock>span").hide();
					$(tmp_idPrefix+"buyqn").val(0);
					$(tmp_idPrefix+"addToCart,"+tmp_idPrefix+"nowbuyToCart").hide();
					$(tmp_idPrefix+"noAddToCart").show();
				}else{
					$(tmp_idPrefix+"stock>em").html(lodo_goods_quantity);
					$(tmp_idPrefix+"stock>span").show();
					$(tmp_idPrefix+"addToCart,"+tmp_idPrefix+"nowbuyToCart").show();
					$(tmp_idPrefix+"noAddToCart").hide();
				}
			}else{
				$(tmp_idPrefix+"stock>em").html(lodo_lang_js.bind_goods.tip_13);
				$(tmp_idPrefix+"stock>span").hide();
			}
		}
	});
	BIND_Meal();
})

function good_cal(s,bind_num,lvtype){
	var qs=s;
	var tempGN=$("#bind_"+bind_num+"_buyqn");
	var maxqn=$("#bind_"+bind_num+"_buy_maxCount").val()*1;
	var bminqn=$("#bind_"+bind_num+"_minbuyqn").val()*1;
	var bmaxqn=$("#bind_"+bind_num+"_maxbuyqn").val()*1;
	var buysum=((tempGN.val()*1)+qs);
	
	if(buysum>maxqn&&maxqn!=-1){alert(lodo_lang_js.bind_goods.tip_14.replace("{$1}",maxqn));return false}
	if(buysum<bminqn){alert(lodo_lang_js.bind_goods.tip_7.replace("{$1}",bminqn));return false}
	if(buysum>bmaxqn){alert(lodo_lang_js.bind_goods.tip_8.replace("{$1}",bmaxqn));return false}
	var buyqn_total=buysum;
	for(var B in lodo_bind_Meal_list){
		if(!$.isEmptyObject(lodo_bind_Meal_list[B])&&bind_num!=B){
			buyqn_total+=lodo_bind_Meal_list[B].buyqn;
		}
	}
	var bind_buyQn_Total=$("#bind_buyQn_Total").val()*1;
	if(buyqn_total>bind_buyQn_Total){alert(lodo_lang_js.bind_goods.tip_9.replace("{$1}",bind_buyQn_Total));return false}
	tempGN.val(buysum);
}